<?php
class entry_index {
    var $db = null;
    var $smarty = null;
    var $msg = null;
	
	function entry_index() {
        $this->smarty = new MySmarty();
        $this->db = new DbAccess();
    }
	
	function displayForm($formvars = array())
	{
		$this->smarty->assign('formvars',$formvars);
		if(isset($formvars['page']) && is_numeric($formvars['page'])){
			$page = $formvars['page'];
		}else{
			$page = 1;
		}
		
		$limit = "limit ".($page-1)*PAGE_SIZE.",".PAGE_SIZE;
		if(isset($formvars['title']) && $formvars['title']!=''){
		    $title = stripslashes($formvars['title']);
			$tag_query = "&title=".urlencode($formvars['title']);
		}else
		{
			$title="";
			$tag_query ="";
		}
		
		
		/**
		 * 総件数
		 */
		if($title!=''){
			$titleParam='%'.$title.'%';
			$sql = "SELECT COUNT(entry_id) FROM entrylist
			        WHERE title like :title";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(':title', $titleParam);
			
		}else{
			$sql = "SELECT COUNT(entry_id) FROM entrylist";
			$stmt = $this->db->prepare($sql);	
		}
		
		$stmt->execute();
		$count =$stmt->fetchColumn();
		$this->smarty->assign('count', $count);
		
		$stmt = null;
		
		/**
		 * 一覧
		 */
		if($title!=''){
			$titleParam='%'.$title.'%';
			$sql = "SELECT * FROM entrylist u 
						left join country_mst c
							on  u.country_id=c.country_id
			     WHERE title like :title
			     ORDER BY entry_id desc
			     $limit";
			$stmt = $this->db->prepare($sql);
			$stmt->bindParam(':title', $titleParam);
			
		}else{
			$sql = "SELECT * FROM entrylist u
						left join country_mst c
							on  u.country_id=c.country_id
			      ORDER BY entry_id desc 
				  $limit";
			$stmt = $this->db->prepare($sql);
				
		}
		$stmt->execute();

		$entryList = $stmt->fetchAll();
		
		$stmt = null;
		
		foreach ($entryList as $key => $row) {
			$sql = "select * from videolist where entry_id=".$row["entry_id"];
			$stmt = $this->db->prepare($sql);
			$stmt->execute();
			$videoArr = $stmt->fetchAll();
			$stmt = null;
			$entryList[$key]["videoList"]=$videoArr;
			$entryList[$key]["videoNum"]=sizeof($videoArr);
		}
		
		
		$this->smarty->assign('entryList', $entryList);
		
		$this->smarty->assign('paging', $this->smarty->getNavigation("entry_index.php",$page,$count,$tag_query));
		$this->smarty->assign('msg', $this->msg);
		
		$this->smarty->display( 'entry_index.tpl' );
	}
	
	function deleteEntry($formvars = array())
	{
		$entry_id=$formvars['entry_id'];
		$count =$this->db->exec("DELETE FROM entrylist WHERE entry_id = '$entry_id'");
		if($count>0)
		{
			$this->msg = "エントリ[$entry_id]の削除に成功しました";
		}
		else
		{
			$this->msg = "エントリ[$entry_id]が削除できません";
		}
		$this->displayForm($formvars = array());
	}
	
	function deleteVideo($formvars = array())
	{
		$checkVideoIdList=$formvars["checkVideoId"];
		$idList = "";
		foreach($checkVideoIdList as $key =>$id)
		{
				$idList.=$key==0?$id:",".$id;
		}
		if($idList == "")
		{
			return;
		}
		
		$idList = "(".$idList.")";
		//echo("DELETE FROM videolist WHERE video_id in '$idList'");
		$count =$this->db->exec("DELETE FROM videolist WHERE video_id in $idList");
		if($count>0)
		{
			$this->msg = "ビデオの削除に成功しました";
		}
		else
		{
			$this->msg = "ビデオを削除できませんでした";
		}
		
		$this->displayForm($formvars = array());
	}
}
?>